<template>
  <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body :close-on-click-modal="false" v-dialogDrag>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
               label-width="100px">
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="queryParams.supplierName" placeholder="请输入供应商名称"
                    clearable @keyup.enter.native="handleQuery"/>
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable v-model="queryParams.payTime"
                          type="date" value-format="yyyy-MM-dd" placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          <el-button size="mini" type="text" icon="el-icon-printer"
                       @click="handlePrint" v-hasPermi="['develop:PayApply:edit']">打印
            </el-button>
        </el-form-item>
      </el-form>

     <div  id="printable-table">
       <el-table border v-loading="loading" :data="dataList" @selection-change="handleSelectionChange" ref="table"
                show-summary :summary-method="getSummaries"
                :max-height="tableMaxHeight">
        <!--序号-->
        <el-table-column label="序号" type="index" width="50" align="center"></el-table-column>
        <el-table-column label="供应商名称" align="center" prop="supplierName"/>
        <el-table-column label="开户行" align="center" prop="bankName"/>
        <el-table-column label="银行户号" align="center" prop="bankNo"/>
        <el-table-column label="账户" align="center" prop="accountNo"/>
        <el-table-column label="支付时间" align="center" prop="payTime" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="付款金额" align="center" prop="money"/>
        <el-table-column  align="center" label="状态" prop="status">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.pay_record_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
          <template slot-scope="scope">
            <el-popconfirm :title="`确认同意撤销该付款单吗？`" v-if="scope.row.status === '0'" @confirm="handleCancelPay(scope.row)"
                           style="margin: 0 10px;">
              <el-button slot="reference" size="mini" type="text" icon="el-icon-remove-outline"> 撤销打款
              </el-button>
            </el-popconfirm>

            <el-button size="mini" type="text" icon="el-icon-document"
                       @click="handleDetail(scope.row)" v-hasPermi="['develop:PayApply:edit']">详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
     </div>
      <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize" @pagination="getList"/>
      <payRecord-form ref="form" @ok="getList"></payRecord-form>
      <pay-record-detail ref="detail" @ok="getList"></pay-record-detail>
      <printer-form ref="PrinterForm"></printer-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>

</template>

<script>
import {accAdd, deleteAction, getAction} from '@/api/manage'
import PayRecordForm from '@/views/develop/payRecord/module/PayRecordForm'
import PayRecordDetail from "@/views/develop/payRecord/module/PayRecordDetail.vue";
import PrinterForm from '@/views/develop/payRecord/module/PrinterForm'

export default {
  name: "PayRecordListModule",
  dicts:["pay_record_status"],
  components: {
    PayRecordDetail,
    PayRecordForm,
    PrinterForm
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      activeName: "first",
      // 弹出层标题
      title: "打款记录",
      // 是否显示弹出层
      open: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 付款记录表格数据
      dataList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        applyId: null,
        supplierId: null,
        supplierName: null,
        payTime: null,
        money: null,
        status: '0',
        orderByColumn: 'payTime',
        isAsc: 'desc',
      },
      url: {
        list: '/payRecord/payRecord/list',
        remove: '/payRecord/payRecord/',
        cancel: '/payRecord/payRecord/cancel/',
        export: '/payRecord/payRecord/export',
      },
      tableMaxHeight: window.innerHeight - 320,
    };
  },
  created() {
    this.getList();
    window.addEventListener('resize', this.getTableHeight);
  },
  destroyed() {
    window.removeEventListener('resize', this.getTableHeight)
  },
  methods: {
    handlePrint() {
     this.$refs.PrinterForm.showPayRecordList(this.queryParams.applyId);

    },
    /** 查询付款记录列表 */
    getList() {
      this.loading = true;
      getAction(this.url.list, this.queryParams).then(response => {
        this.dataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getTableHeight() {
      this.tableHeight = window.innerHeight - 320
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.form.handleAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$refs.form.handleUpdate(row);
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      deleteAction(this.url.remove, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(this.url.export, {
        ...this.queryParams
      }, `payRecord_${new Date().getTime()}.xlsx`)
    },

    showPayRecordList(applyId){
      this.open = true;
      this.queryParams.applyId = applyId;
      this.getList();
    },

    handleCancelPay(row){
      const ids = row.id || this.ids;
      deleteAction(this.url.cancel, ids).then(response => {
        this.getList();
        this.$modal.msgSuccess("取消成功");
      }).catch(() => {
      });

    },

    getSummaries(param) {
      const {columns, data} = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map((item) => Number(item[column.property]));
        // 只合计money，想合并哪些列就在这里指定 排除status为2的
        if ( column.property === "money") {
            sums[index] = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return accAdd(prev , curr);
              } else {
                return prev;
              }
            }, 0);
        }
      });
      return sums;
    },

    handleDetail(row){
      this.$refs.detail.showDetail(row);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.$emit('ok');
    },




  }
};
</script>
